package com.qing.springcloud.config.handle;
import java.io.IOException;
import java.lang.reflect.Type;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.LongCodec;
import com.alibaba.fastjson.serializer.SerializeWriter;

/**
 * @author caozhipeng
 * @date 2025/7/21
 */
public class LongCodeHandle extends LongCodec{

    public static final LongCodeHandle instance = new LongCodeHandle();

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                      int features) throws IOException {
        SerializeWriter out = serializer.out;

        if (object == null) {
            out.writeNull();
            return;
        }

        String strVal = object.toString();

        if (strVal.length() > 16) {
            out.writeString(strVal);
        } else {
            out.writeLong(((Long) object).longValue());
        }
    }



}
